package middleware

import (
	"github.com/gin-gonic/gin"
	"github.com/ulule/limiter/v3"
	mgin "github.com/ulule/limiter/v3/drivers/middleware/gin"
	"github.com/ulule/limiter/v3/drivers/store/memory"
	"interview/pkg/global"
	"time"
)

func RateLimiter() gin.HandlerFunc {
	rate := limiter.Rate{
		Period: time.Second,
		Limit:  global.Conf.RateLimit.Max,
	}

	// 将数据存入in-memory
	store := memory.NewStore()

	instance := limiter.New(store, rate, limiter.WithTrustForwardHeader(true))

	return mgin.NewMiddleware(instance)
}
